package com.hoshino.momochi.controller;

import com.hoshino.momochi.model.DTO.MessageDTO;
import com.hoshino.momochi.model.domain.Message;
import com.hoshino.momochi.service.MessageService;
import com.hoshino.momochi.util.CodeEnum;
import com.hoshino.momochi.util.JsonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

@RestController
@Slf4j
public class MessageController {

    private final String USER_ID = CodeEnum.USERID.getMessage();

    @Autowired
    private MessageService messageService;

    /**
     * 获取用户的所有消息
     *
     * @param request
     * @return
     */
    @RequestMapping("api/message/uid")
    public JsonResponse getUserIdMessage(HttpServletRequest request) {
        Long uid = (Long) request.getAttribute(USER_ID);
        List<Message> messageList = messageService.findMessageByUserId(uid);
        return JsonResponse.success(messageList, CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 获取与 某人 的聊天记录 传入字段 pull_id
     *
     * @param request
     * @param messageDTO
     * @return
     */
    @RequestMapping("api/message/getUser")
    public JsonResponse getMessageByUser(HttpServletRequest request, @RequestBody MessageDTO messageDTO) {
        Long uid = (Long) request.getAttribute(USER_ID);
        Long aid = messageDTO.getPull_id();
        if (Objects.isNull(aid)) {
            return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
        }
        List<Message> res = messageService.getMessageByUser(aid, uid);
        return JsonResponse.success(res, CodeEnum.SUCCESS.getMessage());
    }

    /**
     * 删除发送者方 此条消息
     *
     * @param request
     * @param messageDTO 需要包含字段 id send_id pull_id(可有可无)
     * @return
     */
    @RequestMapping("api/message/delsend")
    public JsonResponse delSendMessage(HttpServletRequest request, @RequestBody MessageDTO messageDTO) {
        Long uid = (Long) request.getAttribute(USER_ID);
        if (uid != messageDTO.getSend_id()) {
            // 身份不符合
            return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
        }
        messageService.updateByIDAndSend(messageDTO.getId());
        return JsonResponse.success(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage());
    }


    /**
     * 删除接收者方 此条消息
     *
     * @return
     */
    @RequestMapping("api/message/delpull")
    public JsonResponse delPullMessage(HttpServletRequest request, @RequestBody MessageDTO messageDTO) {
        Long uid = (Long) request.getAttribute(USER_ID);
        if (uid != messageDTO.getPull_id()) {
            // 身份不符合
            return JsonResponse.success(CodeEnum.ERROR.getCode(), CodeEnum.ERROR.getMessage());
        }
        messageService.updateByIDAndPull(messageDTO.getId());
        return JsonResponse.success(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage());
    }
}
